import { dialog } from 'electron'
import { initStoreFiles } from '../../utils/createStore'
import { IStoreFilesParams } from '../../common/types/file'
import fs from 'fs'
class FileOperationApi {
  // 1. 处理文件保存对话框
  static async showSaveDialog() {
    const result = await dialog.showOpenDialog({
      properties: ['openDirectory'], // 打开文件夹
      title: '请选择文件夹',
      buttonLabel: '选择文件夹'
    })
    return result
  }
  // 2. 处理文件写入（保持不变）
  static async writeFile(filePath: string, content: IStoreFilesParams) {
    try {
      initStoreFiles(filePath, content)
      return { success: true, message: '文件写入成功' }
    } catch (error: any) {
      return { success: false, message: `写入失败：${error.message}` }
    }
  }
  // 获取当前文件夹下所有文件名称
  static getAllFiles(dir: string) {
    try {
      const files = fs.readdirSync(dir)
      return files
    } catch (error) {
      return []
    }
  }
  //获取文件内容
  static getFileContent(filePath: string) {
    try {
      const content = fs.readFileSync(filePath, 'utf8')
      return content
    } catch (error) {
      return null
    }
  }
}
export default FileOperationApi
